package com.qudian.pay.core.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 通用返回对象
 * @author gaoxiang
 * @date 2017年5月31日 下午5:01:21
 */
public class CommonResp<T> implements Serializable {
	
	private static final long serialVersionUID = 3162052777857581305L;
	
	/**
	 * @Fields status : 执行状态
	 */
	private String code;
	
	/**
	 * @Fields msg : 消息
	 */
	private String msg;
	
	/**
	 * @Fields data : 数据
	 */
	private T data;
	
	private Map<String, Object> extraData = new HashMap<String, Object>();
	
	public CommonResp() {
	}
	
	public static <T> CommonResp<T> newInstance() {
		return new CommonResp<T>();
	}
	
	public CommonResp(String code, String msg) {
		super();
		this.code = code;
		this.msg = msg;
	}
	
	public CommonResp(String code, String msg, T data) {
		super();
		this.code = code;
		this.msg = msg;
		this.data = data;
	}
	
	public String getCode() {
		return code;
	}
	
	public void setCode(String code) {
		this.code = code;
	}
	
	public String getMsg() {
		return msg;
	}
	
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	public T getData() {
		return data;
	}
	
	public void setData(T data) {
		this.data = data;
	}
	
	public CommonResp<T> success() {
		this.code = "200";
		this.msg = "成功";
		return this;
	}
	
	public CommonResp<T> success(T data) {
		this.code = "200";
		this.msg = "成功";
		this.data = data;
		return this;
	}
	
	public CommonResp<T> fail() {
		this.code = "400";
		this.msg = "失败";
		return this;
	}
	
	public CommonResp<T> fail(String msg) {
		this.code = "400";
		this.msg = msg;
		return this;
	}
	
	public CommonResp<T> exception() {
		this.code = "500";
		this.msg = "失败";
		return this;
	}
	
	public CommonResp<T> exception(String msg) {
		this.code = "500";
		this.msg = msg;
		return this;
	}
	
	public boolean isSuccess() {
		return "200".equals(this.code);
	}
	
	public boolean isFailure() {
		return !isSuccess();
	}
	
	public boolean isException() {
		return "500".equals(this.code);
	}
	
	public <P> void setExtraData(String key, P value) {
		extraData.put(key, value);
	}
	
	@SuppressWarnings("unchecked")
	public <P> P getExtraData(String key) {
		return (P) extraData.get(key);
	}
	
	public Map<String, Object> getExtraData() {
		return extraData;
	}
	
	public void setExtraData(Map<String, Object> extraData) {
		this.extraData = extraData;
	}
}
